package com.me.components {
	import com.me.FileLoader;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-16 下午03:48:48
	 */
	public class LocalImage extends MapResource {
		private var _spName:TextField = new TextField();
		private var _spBit:Bitmap;
		private var _dragHandler:Function;
		private var _stopDragHandler:Function;

		public function LocalImage(file:File, handler:Function, dragHandler:Function, stopDragHandler:Function) {
			super();
			_fileName = file.name;
			_loadCompleteHandler = handler;
			_dragHandler = dragHandler;
			_stopDragHandler = stopDragHandler;
			_spName.type = TextFieldType.INPUT;
			_spName.autoSize = TextFieldAutoSize.CENTER;
			FileLoader.getInstance().loadFile(file, fileLoaded);
		}

		private function fileLoaded(data:Bitmap):void {
			try {
				width = data.width;
				height = data.height;
				useHandCursor = true;
				var bd:BitmapData = new BitmapData(data.width, data.height);
				bd.copyPixels(data.bitmapData, data.bitmapData.rect, new Point(0, 0));
				_spBit = new Bitmap(bd);
				_spBit.y = 0 - bd.height;
				_spBit.x = 0 - bd.width / 2;
				_spName.text = "name";
				_spName.width = this.width;
				_spName.y = _spBit.y - 20;
				_spName.x = 0 - (this.width - _spName.width) / 2;
				_spName.backgroundColor = 0xff0000;
				addChild(_spBit);
				addChild(_spName);
				x = 300;
				y = 300;
				addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
				_loadCompleteHandler(this);
			} catch (e:Error) {

			}
		}

		private function onMouseDownHandler(e:MouseEvent):void {
			addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			_dragHandler();
			startDrag(false);
		}

		private function onMouseUpHandler(e:MouseEvent):void {
			removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			_stopDragHandler();
			stopDrag();
		}
	}
}
